package org.deyang.tools.amazon;

import java.io.IOException;
import java.util.Iterator;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;



public class PriceHistoryQueryServlet extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4541918034432427783L;
	private static final Logger log = Logger.getLogger(PriceHistoryQueryServlet.class.getName());
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp){
		Object parseResult = null;
		parseResult = JSONValue.parse(req.getParameter("asinlist"));
		resp.setContentType("application/json; charset=UTF-8");
		if (parseResult instanceof JSONArray){
			Iterator<JSONObject> asinIter = ((JSONArray)parseResult).iterator();
			JSONArray resultArray = new JSONArray();
			while (asinIter.hasNext()){
				JSONObject asinEntry = asinIter.next();

//				KeepaPriceAnalyzer priceTracker = new KeepaPriceAnalyzer((String) asinEntry.get("asin"), ((Long)asinEntry.get("priceint")).intValue());
//				try {
//					if (priceTracker.isDeal(KeepaPriceAnalyzer.AMAZON_SELF, 30)){
//						resultArray.add(asinEntry);
//					}
//				} catch (KeepaContentException e) {
//					log.warning("Get error when check price history" + asinEntry.get("asin"));
//				}
			}
			try {
				resp.getWriter().print(resultArray.toString());
				resp.getWriter().flush();
			} catch (IOException e) {
				log.warning(e.getMessage());
			}
			
		}
		
		

	}
}
